<?php

namespace App\Traits;

use Illuminate\Http\Request;

trait CurdTrait
{
    /**
     * @var string[] 需要排除的字段
     */
    protected $curdExceptFields = ['user_id', 'access_token', 'page', 'pageSize'];

    /**
     * @var \Illuminate\Database\Eloquent\Model
     */
    protected $curdModel;

    public function setCurdModel($curdModel)
    {
        $this->curdModel = $curdModel;
    }

    /**
     * 列表
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/13
     */
    public function list(Request $request)
    {
        $where = $request->except($this->curdExceptFields);
        $list = $this->curdModel->newQuery()
            ->where($where)
            ->paginate($this->pageSize);

        return $this->responseSuccess('', $list);
    }

    /**
     * 详情
     * @param \Illuminate\Http\Request $request
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function info(Request $request)
    {
        $pk = $this->curdModel->getKeyName();
        $id = $request->input($pk);
        $model = $this->curdModel->newQuery()->find($id);
        if (empty($model)) {
            return $this->responseFail('数据不存在');
        }

        return $this->responseSuccess('', $model);
    }

    /**
     * 添加
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function add(Request $request)
    {
        $saveData = $request->except($this->curdExceptFields);

        $res = $this->curdModel->fill($saveData)->save();
        if (!$res) {
            return $this->responseFail('添加失败');
        }

        return $this->responseSuccess('添加成功');
    }

    /**
     * 编辑
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function edit(Request $request)
    {
        $pk = $this->curdModel->getKeyName();
        $id = $request->input($pk);
        if (empty($id)) {
            return $this->responseFail('缺少主键');
        }

        $model = $this->curdModel->newQuery()->find($id);
        if (empty($model)) {
            return $this->responseFail('数据不存在');
        }

        $saveData = $request->except($this->curdExceptFields);
        $res = $model->fill($saveData)->save();
        if (!$res) {
            return $this->responseFail('编辑失败');
        }

        return $this->responseSuccess('编辑成功');
    }

    /**
     * 保存
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function save(Request $request)
    {
        $pk = $this->curdModel->getKeyName();
        $id = $request->input($pk);

        // 编辑
        if ($id) {
            $model = $this->curdModel->newQuery()->find($id);
            if (empty($model)) {
                return $this->responseFail('数据不存在');
            }

            $saveData = $request->except($this->curdExceptFields);
            $res = $model->fill($saveData)->save();
            if (!$res) {
                return $this->responseFail('保存失败');
            }
        }
        // 新增
        else {
            $saveData = $request->except(array_merge($this->curdExceptFields, [$pk]));

            $res = $this->curdModel->fill($saveData)->save($saveData);
            if (!$res) {
                return $this->responseFail('保存失败');
            }
        }

        return $this->responseSuccess('保存成功');
    }

    /**
     * 删除
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function delete(Request $request)
    {
        $pk = $this->curdModel->getKeyName();
        $id = $request->input($pk);
        if (empty($id)) {
            return $this->responseFail('缺少主键');
        }

        $model = $this->curdModel->newQuery()->find($id);
        if (empty($model)) {
            return $this->responseFail('数据不存在');
        }

        $res = $model->delete();
        if (!$res) {
            return $this->responseFail('删除失败');
        }

        return $this->responseSuccess('删除成功');
    }
}
